package com.novel.app.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.dongdxiang.framework.BaseActivity;
import com.dongdxiang.framework.http.QjResult;
import com.google.gson.reflect.TypeToken;
import com.novel.app.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;


public class RgActivity extends BaseActivity {

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_rg);
        Button bt = (Button) findViewById(R.id.bt_submit);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doNet();
            }
        });

    }


    private void doNet(){
        String url = "http://s-338727.gotocdn.com/api/register";
       // httpGetRequest(0,url,new TypeToken<QjResult<Map<String,Object>>>() {},true,true);

        JSONObject jsonObject = new JSONObject();
        try {
            EditText et_uname = (EditText)findViewById(R.id.et_uname);
            String uname = et_uname.getText().toString().trim();
            EditText et_upass = (EditText)findViewById(R.id.et_upass);
            String upass = et_upass.getText().toString().trim();
            if (!(uname!=null&&uname.length()>0)){
                Toast.makeText(this,"请输入用户名",Toast.LENGTH_SHORT).show();
                return;
            }else if (!(upass!=null&&upass.length()>0)){
                Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
                return;
            }
            jsonObject.put("username",uname);
            jsonObject.put("password",upass);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        httpPostRequest(0,url,new TypeToken<QjResult<Map<String,Object>>>(){},true,true,jsonObject);
    }

    @Override
    protected void onSucceed(int what, Object response) {
        if (response != null){
            Map<String,Object> map = (Map<String,Object>)response;
            Toast.makeText(this,"注册成功！",Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    protected void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
        System.out.println(responseCode);
        exception.printStackTrace();

    }

}
